package com.yamaha.bikedesalon;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

import com.sromku.simple.fb.Permissions;
import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.SimpleFacebookConfiguration;

public class AActivity extends Activity {

	private boolean isRegisterd = false;
	private final Object syncObject = new Object();

	protected SimpleFacebook simpleFacebook;

	protected final Permissions[] permissions = new Permissions[]{
			Permissions.EMAIL, Permissions.USER_ABOUT_ME,
			Permissions.USER_ACTIVITIES, Permissions.USER_BIRTHDAY,
			Permissions.USER_EDUCATION_HISTORY, Permissions.USER_GROUPS,
			Permissions.USER_HOMETOWN, Permissions.USER_INTERESTS,
			Permissions.USER_LIKES, Permissions.USER_LOCATION,
			Permissions.USER_SUBSCRIPTIONS, Permissions.USER_WEBSITE,
			Permissions.USER_WORK_HISTORY, Permissions.USER_STATUS,
			Permissions.READ_STREAM, Permissions.READ_FRIENDLISTS,
			Permissions.USER_PHOTOS, Permissions.FRIENDS_PHOTOS,
			Permissions.PUBLISH_ACTION};

	private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String newMessage = intent.getExtras().getString(CommonUtilities.EXTRA_MESSAGE);
			// Waking up mobile if it is sleeping
			WakeLocker.acquire(getApplicationContext());

			LogUtil.v(getClass().getSimpleName(), "Display Notification : " + newMessage);
			Toast.makeText(getApplicationContext(), newMessage, Toast.LENGTH_LONG).show();

			/**
			 * Take appropriate action on this message depending upon your app
			 * requirement For now i am just displaying it on the screen
			 * */

			// Releasing wake lock
			WakeLocker.release();
		}
	};

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LogUtil.v(getClass().getSimpleName(), "Enter Create Mode.");

		synchronized (syncObject) {
			if (!isRegisterd) {
				registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_MESSAGE_ACTION));
				isRegisterd = true;
			}
		}

		final SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder().setAppId(getResources().getString(R.string.app_id)).setNamespace("yamaha_socialbike").setPermissions(permissions).build();
		SimpleFacebook.setConfiguration(configuration);

		simpleFacebook = SimpleFacebook.getInstance(this);
	}

	@Override
	protected void onDestroy() {

		LogUtil.v(getClass().getSimpleName(), "Enter Destroy Mode.");

		synchronized (syncObject) {
			if (isRegisterd) {
				unregisterReceiver(mHandleMessageReceiver);
				isRegisterd = false;
			}
		}

		Utils.closeAllDialog();

		super.onDestroy();
	}

	@Override
	protected void onPause() {

		LogUtil.v(getClass().getSimpleName(), "Enter Pause Mode.");

		synchronized (syncObject) {
			if (isRegisterd) {
				unregisterReceiver(mHandleMessageReceiver);
				isRegisterd = false;
			}
		}

		Utils.closeAllDialog();

		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		simpleFacebook = SimpleFacebook.getInstance(this);

		LogUtil.v(getClass().getSimpleName(), "Enter Resume Mode.");

		synchronized (syncObject) {
			registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_MESSAGE_ACTION));
			isRegisterd = true;
		}
	}
}
